@using System.Globalization
@using BTCPayServer.Payments
@using BTCPayServer.Payments.Bitcoin
@using BTCPayServer.Services
@using BTCPayServer.Services.Invoices
@using NBitcoin
@inject DisplayFormatter DisplayFormatter
@inject TransactionLinkProviders TransactionLinkProviders
@inject PaymentMethodHandlerDictionary handlers
@model InvoiceDetailsModel
@{
	PayjoinInformation payjoinInformation = null;
	var payments = Model.Payments
		.Select(payment =>
        {
			if (!handlers.TryGetValue(payment.PaymentMethodId, out var h) || h is not BitcoinLikePaymentHandler handler)
				return null;
			var network = handler.Network;
            var m = new OnchainPaymentViewModel();
            var onChainPaymentData = handler.ParsePaymentDetails(payment.Details);
            m.PaymentMethodId = payment.PaymentMethodId;
            m.DepositAddress = BitcoinAddress.Create(payment.Destination, network.NBitcoinNetwork);

            var confReq = NBXplorerListener.ConfirmationRequired(payment.InvoiceEntity, onChainPaymentData);
            var confCount = onChainPaymentData.ConfirmationCount;
            confCount = Math.Min(confReq, confCount);
            m.Confirmations = $"{confCount} / {confReq}";
            if (onChainPaymentData.PayjoinInformation is PayjoinInformation pj)
            {
                payjoinInformation = pj;
                m.AdditionalInformation = StringLocalizer["Original transaction"];
            }
            if (payjoinInformation is PayjoinInformation &&
                payjoinInformation.CoinjoinTransactionHash == onChainPaymentData?.Outpoint.Hash)
            {
                m.AdditionalInformation = StringLocalizer["Payjoin transaction"];
            }
            m.TransactionId = onChainPaymentData.Outpoint.Hash.ToString();
            m.ReceivedTime = payment.ReceivedTime;
            m.TransactionLink = TransactionLinkProviders.GetTransactionLink(payment.PaymentMethodId, onChainPaymentData.Outpoint.ToString());
            m.Replaced = !payment.Accounted;
            m.CryptoPaymentData = onChainPaymentData;
			m.Value = payment.Value;
            m.NetworkFee = payment.PaymentMethodFee;
            m.PaymentProof = onChainPaymentData.Outpoint.ToString();
            m.Currency = payment.Currency;
            return m;
        })
        .Where(model => model != null)
        .ToList();
}

@if (payments.Any())
{
    var hasNetworkFee = payments.Sum(a => a.NetworkFee) > 0;
    <section>
        <h5 text-translate="true">On-Chain Payments</h5>
        <div class="invoice-payments table-responsive mt-0">
            <table class="table table-hover mb-0">
                <thead>
                <tr>
                    <th text-translate="true" class="w-75px">Payment Method</th>
                    <th text-translate="true" class="w-100px">Index</th>
                    <th text-translate="true" class="w-175px">Destination</th>
                    <th text-translate="true" class="text-nowrap">Payment Proof</th>
                    @if (hasNetworkFee)
                    {
                        <th class="text-end">
                            <span text-translate="true">Network Fee</span>
                            <a href="https://docs.btcpayserver.org/FAQ/Stores/#allow-anyone-to-create-invoice" target="_blank" rel="noreferrer noopener" title="@StringLocalizer["More information..."]">
                                <vc:icon symbol="info" />
                            </a>
                        </th>
                    }
                    <th text-translate="true" class="text-end">Confirmations</th>
                    <th text-translate="true" class="w-150px text-end">Paid</th>
                </tr>
                </thead>
                <tbody>
                @foreach (var payment in payments)
                {
                    <tr style="@(payment.Replaced ? "text-decoration: line-through" : "")">
                        <td>@payment.PaymentMethodId</td>
                        <td>@(payment.CryptoPaymentData.KeyPath?.ToString() ?? payment.CryptoPaymentData.KeyIndex?.ToString() ?? StringLocalizer["Unknown"])</td>
                        <td>
                            <vc:truncate-center text="@payment.DepositAddress.ToString()" classes="truncate-center-id" />
                        </td>
                        <td>
                            <vc:truncate-center text="@payment.PaymentProof" link="@payment.TransactionLink" classes="truncate-center-id" />
                        </td>
                        @if (hasNetworkFee)
                        {
                            <td class="text-end text-nowrap">@payment.NetworkFee</td>
                        }
                        <td class="text-end">@payment.Confirmations</td>
                        <td class="payment-value text-end text-nowrap">
                            <span data-sensitive class="text-success">@DisplayFormatter.Currency(payment.Value, payment.Currency)</span>
                            @if (!string.IsNullOrEmpty(payment.AdditionalInformation))
                            {
                                <div>(@payment.AdditionalInformation)</div>
                            }
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    </section>
}
